<template>
	<view class="near">



		<u-navbar id="tabbars" :is-back="true" :border-bottom="false" back-icon-color="#fff" title="租借氧气瓶"
			title-size="36" title-color="#fff" :background="background">

			<!-- <view class="slot-wrap">
				<image @click="goback" class="back" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/21.png"
					mode=""></image>
			</view> -->
		</u-navbar>

		<view :style="{'top': totalHeight+'px'}" class="decoration">
			<image class="imgs" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/c1.png" mode=""></image>
		</view>


		<view class="content">
			<view class="info">
				<view v-if="flagStatus" class="tit">
					{{device.shop_name}}
				</view>
				<view v-if="flagStatus" class="guizi">
					<view class="item" :key="index" v-for="(item,index) in list">

						<view @click="handle(item)" v-if="item.canUse" class="ok">
							<view class="photo">
								<image class="img" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b1.png"
									mode=""></image>


							</view>
							<view class="right">
								<view class="num">
									{{item.num}} {{item.canUseMsg}}
								</view>
								<image v-if="item.flag" class="icon"
									src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b2.png" mode=""></image>

								<image v-else class="icon"
									src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b4.png" mode=""></image>
							</view>
						</view>
						<view v-else class="ok not">
							<view class="photo">
								<image class="img" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b9.png"
									mode=""></image>
							</view>
							<view class="right">
								<view class="num">
									{{item.num}} {{item.canUseMsg}}
								</view>
								<image class="icon" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b8.png"
									mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view v-else class="emptys">
					<image style="width: 600rpx; height: 600rpx; display: block; margin: 90px auto 0px;"
						src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/1.gif" mode=""></image>
					<view class="info1">
						{{info}}
					</view>
				</view>
			</view>


			<view v-if="flagStatus" class="scan">
				<view class="tit">
					<view class="left">
						已选择氧气罐
					</view>
					<view class="right">
						{{review.length}}个
					</view>
				</view>


				<view class="ul">
					<view v-for="item in review" class="li">

						<image @click="del(item)" class="del"
							src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b6.png" mode=""></image>

						<view class="name">
							{{item.num}}柜
						</view>
						<view class="money">
							{{singlePrice}}<text>元</text>
						</view>
					</view>

				</view>


				<view class="tips1">
					<image class="imgs" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b3.png" mode=""></image>
					<view class="detail">
						{{priceInfo}}
					</view>
				</view>
			</view>

			<!-- <view class="tips1 dif">
				<image class="imgs" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/b3.png" mode=""></image>
				<view class="detail">
					我已阅读并了解<text>“益生氧”租赁协议</text>
				</view>
			</view> -->





		</view>

		<view v-if="flagStatus" class="foots">
			<view class="left">
				<view class="top">
					需支付总押金金额
				</view>
				<view class="bot">
					{{((review.length)*(singlePrice*1)).toFixed(2)}}
					<text>元</text>
				</view>
			</view>
			<view @click="pay" class="right">

				<text>确认租借</text>
			</view>
		</view>

		<!-- <paySuc v-if="paySucFlag" /> -->

		<u-popup :mask-close-able="false" border-radius="29" v-model="paySucFlag" mode="center">
			<view class="qq">
				<paySuc @handleSuc="handleSuc" />
			</view>
			<!-- <image @click="paySucFlag=false" class="close" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/32.png" mode=""></image> -->
		</u-popup>
	</view>
</template>

<script>
	import paySuc from './paySuc.vue'
	export default {

		watch: {
			list: {
				handler(n, o) {
					console.log(22222222222);
					this.review = this.list?.filter(r => r.flag)
				},
				immediate: true,
				deep: true
			}
		},

		components: {
			paySuc
		},
		data() {
			return {
				paySucFlag: false,
				singlePrice: 389,
				priceInfo: "",
				deviceId: "",
				info: '设备离线!请稍后再试...',
				device: {},
				flagStatus: true,
				review: [],
				list: [
					// 	{
					// 	id: 1,
					// 	num: '01',
					// 	canUse: true,
					// 	flag: false
					// }, {
					// 	id: 2,
					// 	num: '02',
					// 	canUse: true,
					// 	flag: false,
					// }, {
					// 	id: 3,
					// 	num: '03',
					// 	canUse: true,
					// 	flag: false,
					// }, {
					// 	id: 4,
					// 	num: '04',
					// 	canUse: false,
					// 	flag: false,
					// }, {
					// 	id: 5,
					// 	num: '05',
					// 	canUse: true,
					// 	flag: false,
					// }, {
					// 	id: 6,
					// 	num: '06',
					// 	canUse: true,
					// 	flag: false,
					// }, {

					// 	id: 7,
					// 	num: '07',
					// 	canUse: true,
					// 	flag: false,
					// }, {
					// 	id: 8,
					// 	num: '08',
					// 	canUse: true,
					// 	flag: false,
					// }, {
					// 	id: 9,
					// 	num: '09',
					// 	canUse: true,
					// 	flag: false,
					// }, {
					// 	id: 10,
					// 	num: '10',
					// 	canUse: false,
					// 	flag: false,
					// }, {
					// 	id: 11,
					// 	num: '11',
					// 	canUse: false,
					// 	flag: false,
					// }, {
					// 	id: 12,
					// 	num: '12',
					// 	canUse: false,
					// 	flag: false,
					// },
				],
				totalHeight: 0, //总高度    
				background: {

					background: 'url(https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/9.png) no-repeat',
					// 还可以设置背景图size属性
					backgroundSize: 'cover',
					// 渐变色
					// backgroundImage: 'linear-gradient(180deg, #417CF4 0%, #84ACFF 100%)'
				}
			}
		},

		onReady() {
			uni.hideTabBar();
			uni.createSelectorQuery().select('#tabbars').boundingClientRect((rect) => {
				console.log(rect.height)
				this.totalHeight = rect.height
			}).exec()
		},
		methods: {
			handleSuc(){
				
				console.log(444444444);
				this.paySucFlag = false
				this.list=[]
				this.getList()
			},
			pay() {
				if (!this.vuex_token) {
					uni.navigateTo({
						url:"/pages/sys/main/login"
					})
					
				}
				if (!this.review.length) {
					this.$u.toast('请选选择需要租赁的氧气柜！')

				} else {
					let that = this
					this.$u.api.addOrder({
							deviceId: this.deviceId,
							slotIds: this.review?.map(r => r.id) ?? []
						})
						.then((res) => {
							if (res.code == 0) {
								// 		let {appId,nonceStr,package,paySign,signType,timeStamp} = res.payment;

								uni.requestPayment({
									timeStamp: res.payment.timeStamp, //后端返回的时间戳
									nonceStr: res.payment.nonceStr, //后端返回的随机字符串
									package: res.payment.package, //后端返回的prepay_id
									signType: res.payment.signType, //后端签名算法,根据后端来,后端MD5这里即为MD5
									paySign: res.payment.paySign, //后端返回的签名
									success(res) {
										console.log('用户支付扣款成功', res)
										// that.$u.toast('用户支付扣款成功')
										that.paySucFlag = true
									},
									fail(res) {
										
									
										console.log('用户支付扣款失败', res)
									}
								})

							}else{
								 that.$u.toast(res.msg)
							}

						});
				}
			},
			del(item) {
				this.list?.map(r => {
					if (item.id == r.id) {
						item.flag = false
					}
				})

				console.log(this.list, 555555555);
				// item.flag = false
				// this.$forceUpdate()
			},
			handle(item) {
				item.flag = !item.flag

				// this.review = this.list?.filter(r=>r.flag)
			},
			closeDk() {
				this.takeMoneyShow = false
			},
			goback() {
				// uni.reLaunch({
				// 	url: "/pages/sys/main/index"
				// })
			},
			getList(){
				this.$u.api.deviceInfo({
						deviceId:this.deviceId
					})
					.then((res) => {
						if (res.code == 0) {
							this.device = res.device
							this.list = res.slotList?.map(r => {
								let canUse1 = ''
								let canUseMsg1 = ''
								if (r.cargo_state == 1) {
									if(r.open_state==0){
										canUse1 = true
										canUseMsg1 = "可租"
									}else{
										canUse1 = false
										canUseMsg1 = "不可租"
									}
								} else if (r.cargo_state == 0) {
									canUse1 = false
									canUseMsg1 = "空"
								} else {
									canUse1 = false
									canUseMsg1 = "未知"
								}
								return {
									id: r.id,
									num: r.lock_no,
									canUse: canUse1,
									canUseMsg: canUseMsg1,
									flag: false
								}
							})
							this.flagStatus = true
							// this.flagStatus =false
						} else {
				
							this.info = res.msg
							this.flagStatus = false
						}
				
					});
			}
		},
		onLoad(e) {
			console.log(e.deviceId, 55555555555);
			this.deviceId = e.deviceId

			this.$u.api.config({
				paramCode: "priceInfo"
			}).then(res => {
				if (res.code == 0) {
					this.priceInfo = res.config
				}
			})
			this.$u.api.config({
				paramCode: "singlePrice"
			}).then(res => {
				if (res.code == 0) {
					this.singlePrice = res.config
				}
			})
			this.getList()



			
		}
	}
</script>


<style>
	page {
		background: #EDF7FF;
	}
</style>
<style scoped lang="scss">
	.info1 {
		text-align: center;
		color: #333;
		font-size: 16px;
	}

	.foots {

		.left {
			.top {
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 28rpx;
			}

			.bot {
				display: flex;
				align-items: flex-end;
				font-size: 58rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #04BC6D;
				line-height: 68rpx;

				text {
					font-size: 22rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #04BC6E;
					line-height: 45rpx;
				}
			}
		}

		width: 750rpx;
		height: 149rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -13rpx 15rpx 0rpx rgba(197, 197, 197, 0.15);
		opacity: 1;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 19rpx 25rpx 16rpx 28rpx;

		.right {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 382rpx;
			height: 86rpx;
			background: linear-gradient(180deg, #0C5CFF 0%, #84ACFF 100%);
			box-shadow: 0rpx 8rpx 9rpx 0rpx rgba(41, 88, 255, 0.23);
			border-radius: 76rpx 76rpx 76rpx 76rpx;
			opacity: 1;

			text {
				font-size: 36rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				line-height: 42rpx;
				text-shadow: 0px 6px 5px rgba(0, 29, 183, 0.2);
				background: linear-gradient(180deg, #FFFFFF 0%, #C2E2FF 100%);
				-webkit-background-clip: text;
				-webkit-text-fill-color: transparent;
			}
		}
	}

	.decoration {
		width: 100%;
		height: 35rpx;
		position: fixed;
		left: 0;
		right: 0;
		z-index: 88;

		.imgs {
			width: 100%;
			height: 35rpx;
		}
	}

	.near {
		background: #EDF7FF;
		// padding-bottom: 30rpx;
		// background: url("https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/c1.png") no-repeat;
		// background-size: 750rpx 189rpx;
	}

	/deep/ .u-drawer__scroll-view {
		height: inherit !important;
	}

	/deep/ .u-mode-center-box {
		background: none !important;
	}

	.close {
		width: 65rpx;
		height: 65rpx;
		display: block;
		// background: rgba(255,255,255,0.8);
		margin: 46rpx auto;
	}

	.tips {

		margin: 50px 0;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-family: PingFang SC-Light, PingFang SC;
		font-weight: 300;
		color: #999999;

		text {
			color: #7F76FF;
		}
	}

	.back {
		width: 30rpx;
		height: 46rpx;
		margin-left: 40rpx;
	}

	.bottoms {
		padding: 20rpx 0 0;
		display: flex;
		// align-items: center;
		justify-content: space-between;

		.left {
			padding-top: 20rpx;
			font-size: 25rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #666666;
			line-height: 29rpx;

			text {
				font-size: 33rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #3B70FF;
				padding: 0 10rpx;
			}
		}

		.right {
			margin-right: -5px;

			.img1 {
				width: 178rpx;
				height: 82rpx;
			}
		}
	}

	.content {
		background: #EDF6FF;
		position: relative;
		padding-bottom: 259rpx;

		.tips1 {
			display: flex;


			.imgs {
				width: 39rpx;
				height: 39rpx;
				margin-right: 13rpx;
				margin-top: 5px;
			}

			.detail {
				flex: 1;
				font-size: 27rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 47rpx;

				text {
					color: #1B6BE3;
				}
			}

			&.dif {
				padding: 16rpx 0 16rpx 41rpx;
				align-items: center;
				position: fixed;
				bottom: 149rpx;
				background: #EDF7FF;
				left: 0;
				right: 0;

				.imgs {
					margin-top: 0px;
				}
			}
		}

		.scan {
			margin: 12rpx 27rpx 0;
			// height: 363rpx;
			background: #FFFFFF;
			border-radius: 11rpx 11rpx 11rpx 11rpx;
			opacity: 1;
			padding: 29rpx 24rpx 19rpx 22rpx;

			.tit {
				margin-bottom: 21rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.left {
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
					line-height: 35rpx;
				}

				.right {
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #04BC6E;
					line-height: 35rpx;
				}
			}

			.ul {
				display: flex;
				flex-wrap: wrap;

				.li {
					position: relative;

					.del {
						width: 38rpx;
						height: 38rpx;
						position: absolute;
						right: -4px;
						top: -4px;
					}

					margin-bottom: 15rpx;
					width: 32%;
					height: 125rpx;
					background: #F3FAFC;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					opacity: 1;
					border: 2rpx solid #B0DCE8;

					&:nth-child(3n-1) {
						margin: 0 2%;

					}

					.name {
						font-size: 27rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
						line-height: 32rpx;
						padding: 14rpx 0 0 23rpx;
					}

					.money {
						font-size: 44rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #04BC6D;
						line-height: 52rpx;
						padding-left: 50rpx;

						text {
							font-size: 22rpx;
							font-family: PingFang SC-Medium, PingFang SC;
							font-weight: 500;
							color: #04BC6E;
							line-height: 26rpx;

						}
					}
				}
			}

		}



		.info {
			margin: 0 27rpx;
			// min-height: 400px;

			.tit {
				padding: 40rpx 0 15rpx 0;
				font-size: 29rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
				line-height: 34rpx;
			}

			.guizi {
				display: flex;
				flex-wrap: wrap;

				.item {
					margin-bottom: 16rpx;
					width: 337rpx;

					// background: #FFFFFF;




					&:nth-child(2n) {
						margin-left: 20rpx;
					}

					.ok {
						background: #FFFFFF;
						height: 144rpx;
						border: 1rpx solid #FFFFFF;
						border-radius: 11rpx 11rpx 11rpx 11rpx;
						display: flex;
						align-items: center;

						.photo .img {
							width: 111rpx;
							height: 114rpx;
							margin: 0 0 0 13rpx;
						}

						.right {
							display: flex;
							align-items: center;

							.num {
								width: 115rpx;
								margin-right: 27rpx;
								font-size: 30rpx;
								font-family: PingFang SC-Medium, PingFang SC;
								font-weight: 500;
								color: #1B6BE3;
								line-height: 35rpx;
							}

							.icon {
								width: 51rpx;
								height: 51rpx;
							}
						}

						&.not {

							border: 1rpx solid rgba(0, 0, 0, 0.14);

							.num {
								color: #999999;
							}
						}
					}
				}
			}

		}
	}
</style>
